# encoding:utf8
"""
@File        : suning_stores.py
@Time        : 2019/7/1814:02
@Author      : zhaoy
@Email       : zhaoyao@shandiangou.cc
@Description : ETL and export to mysql
"""

import pandas as pd

from etl.utils import *


class SuningStore(object):

    def __init__(self):
        self.db = mongo_client()
        self.engine = mysql_client()

    def extract_data(self, collection):
        data = self.db.get_collection(collection).find({}, {'_id': 0, 'depth': 0, 'download_timeout': 0, 'proxy': 0,
                                                            'download_slot': 0, 'download_latency': 0,
                                                            'goodsSalePoint': 0})
        self.df = pd.DataFrame(list(data))

    def etl(self):
        print(self.df.dtypes)
        self.df['poiId'] = self.df['poiId'].astype(int)
        # self.df['poiName'] = self.df['poiName'].replace(r'\(', '-',regex=True)
        # self.df['poiName'] = self.df['poiName'].replace(r'\)', '',regex=True)
        # print(self.df['goodsSalePoint'])
        # print(self.df['goodsSalePoint'].isna())
        # print(self.df['sameGoodsList'].isna())
        # print(self.df['sameGoodsList'].values)
        # print(self.df['sameGoodsList'].isnull())
        self.df.fillna('', inplace=True)
        # print(self.df['goodsSalePoint'])

    def export2mysql(self, dst):
        self.df.to_sql(name=dst, con=self.engine, if_exists='append', index=True, index_label='id', method='multi')

    def run(self, src, dst):
        self.extract_data(src)
        self.etl()
        self.export2mysql(dst)


if __name__ == '__main__':
    # SuningStore().run('suning_store', 'suning_store')
    SuningStore().run('suning_goods', 'suning_goods')
